package algorithm;

/**
 * 长按键入
 */
public class T00925 {
    public static void main(String[] args) {
        System.out.println(new Solution().isLongPressedName("pyplrz", "ppyypllr"));
    }

static class Solution {
    public boolean isLongPressedName(String name, String typed) {
        int nameLength, typedLength;
        if(name == null
                || typed == null
                || (typedLength = typed.length()) < (nameLength = name.length()))
            return false;

        if(nameLength == 0 && nameLength == typedLength)
            return true;

        char[] arr1 = name.toCharArray(), arr2 = typed.toCharArray();

        int nameIndex = 0, typedIndex = 0;
        while(typedIndex < typedLength && nameIndex < nameLength) {
            if(arr1[nameIndex] == arr2[typedIndex]) {
                nameIndex ++;
                typedIndex ++;
            } else if(nameIndex > 0 && arr2[typedIndex] == arr1[nameIndex - 1]) {
                typedIndex ++;
            } else
                return false;
        }
        if(typedIndex == typedLength)
            return nameIndex == nameLength || (nameIndex == nameLength - 1 && arr1[nameIndex] == arr2[typedIndex - 1]);
        else {
            if(nameIndex == nameLength) {
                char nc = arr1[nameIndex - 1];
                while (typedIndex < typedLength && arr2[typedIndex] == nc)
                    typedIndex++;
                return typedIndex == typedLength;
            }
            return false;
        }
    }
}
}